//判断是安卓还是苹果			 			
var u = navigator.userAgent,
app = navigator.appVersion;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //g	
var elemtVideo = '';
if (isAndroid) {
    console.log("安卓机！");
	$('#my-video').hide();
    $('#my-video')[0].pause();
    $('#player').show();
    $('#player')[0].play();
    elemtVideo = $('#player');
} else {
    $('#player')[0].pause();
    $('#my-video').show();
    $('#player').hide();
    $('#my-video')[0].play();
    elemtVideo = $('#my-video');
}
var giftswiper=null,clock=null;
var isplayer=0;

var $loading = $('#loading'),$commentList = $('#indexlist'), totalHeight = 0, timer = null,jqueryXhr = null; // jquery-ajax 对象
var isloading=true;
function getvideoxiangxi(){
	var vinfo=obj.videoData[obj.current];
	$.ajax({
		type: "post",
		dataType: "json",	
		data:{"fid":vinfo.fid,"submitok":"ajax_click"},
		url: mobanurl+"?t="+new Date(),
		cache:false,
		async:false, 
		success: function(d){
			obj.videoData[obj.current].videoviews=parseInt(obj.videoData[obj.current].videoviews)+1;
			obj.videoData[obj.current].click=parseInt(obj.videoData[obj.current].click)+1;
		}
	});
}
function goPlay(e) {
    var e = e || event;
    e.cancelBubble = true;
    var that = e.target || e.srcElement;
    if (that.isFirstLoadVideo) {
        that.mySwiper.unlockSwipeToNext();
        that.isFirstLoadVideo = false;
    }
    if (isAndroid) {
        var ele = document.getElementById('player');
    } else {
        var ele = document.getElementById('my-video');
    }
    var paused = ele.paused;
    var ended = ele.ended;
    var readyState = ele.readyState;
    if (paused) {
        ele.play();
        $(that).parent().find('.play_btn').hide();
        $(that).parent().find('.posterImg').hide();
        that.playBtnStatus = true;
        if (that.readyState) {
            that.playerStatus = true;
        } else {
            if (isAndroid) {               
                setTimeout(function() { that.playerStatus = true; },1500)
            } else {
                that.playerStatus = true;
            }
        }
    } else {
        ele.pause();
        that.playerStatus = false;
        that.playBtnStatus = false;
        that.readyState = true;
        $(that).parent().find('.play_btn').show();
    }

}
function GetVideoState(){ clearInterval(clock);}
function audioAutoPlay(id){
	var audio = document.getElementById(id),
		play = function(){ audio.play(); document.removeEventListener("touchstart",play, false);};
	audio.oncanplay=function(){ setTimeout(function() {$("#nav_btn").removeClass('loading');},1500);}
	audio.play();
	document.addEventListener("WeixinJSBridgeReady", function () { play(); }, false);
	document.addEventListener('YixinJSBridgeReady', function() { play();	}, false);
	document.addEventListener("touchstart",play, false);
}
$(function() {
	$('input').blur(function(){
		var odiv=document.createElement('div')
		odiv.style.height="999px"
		document.body.appendChild(odiv)
		setTimeout(function(){
			odiv.parentNode.removeChild(odiv)
		},0)
	});
    $('.play_btn').hide();
    $('.header_pic_btn').bind('click',function() {
		var gainfo=obj.videoData[obj.current];
		var gzobj=$(this);
		$.ajax({
			type: "post",dataType: "json",
			data:{"uid":gainfo.uid,"submitok":"ajax_gz"},
			url: domainurl+"/m/u.php?version="+new Date(),
			cache:false,async:false, 
			success: function(d){
				if(d.flag==1) {				
					gzobj.addClass('active');
					$(this).find('.guanzhu_icon').addClass('rotateIn');
					setTimeout(function() { $('.header_pic_btn').find('.guanzhu_icon').removeClass('rotateIn');},600);
							
				}else{
					gzobj.removeClass('active');
					$(this).find('.guanzhu_icon').addClass('rotateIn');
					setTimeout(function() { $('.header_pic_btn').find('.guanzhu_icon').removeClass('rotateIn');},600);
					
				}	
			}
		})
        
    });
	$('.dianzan_btn').bind('click',function() {		
		var dzinfo=obj.videoData[obj.current];
		var dzobj=$(this);
		if(dzinfo.dzflag==1){return false;}
		$.ajax({
			type: "post", dataType: "json",
			data:{"uid":dzinfo.uid,"submitok":"ajax_agree","fid":dzinfo.videoid},
			url: domainurl+"/m/video.php?version="+new Date(),
			cache:false, async:false, 
			success: function(d){
				if(d.flag==1) {	
					dzobj.find('.txt').html(parseInt(dzobj.find('.txt').html())+1);
					obj.videoData[obj.current].dzflag=1;
					obj.videoData[obj.current].viewzans=parseInt(obj.videoData[obj.current].viewzans)+1;
					obj.videoData[obj.current].agree=parseInt(obj.videoData[obj.current].agree)+1;			
				}	
			}
		});        
    });		
    $('#music_btn').bind('click',function() {
        if ($(this).hasClass('active')) {
            $(this).removeClass('active');
            if (isAndroid) {
                $('#player')[0].pause();
            } else {
                $('#my-video')[0].pause();
            }
            $('.play_btn').show();
        } else {
            $(this).addClass('active');
            if (isAndroid) {
                $('#player')[0].play();
            } else {
                $('#my-video')[0].play();
            }
            $('.play_btn').hide();
        }
    });	 
	$('.pinglun_select_alert .btn-back').bind('click', function() {pinglunClose();});
     window.onresize = function() {
		if($(window).width()>$(window).height()){
			mySwiper2.changeDirection('horizontal');
			$('.video_ele').width($(window).height());
			$('.video_ele').height($(window).width());
		}else{
			mySwiper2.changeDirection('vertical');
			$('.video_ele').width($(window).width());
			$('.video_ele').height($(window).height());
		}
    } 	
     var mySwiper2 = new Swiper('#shipin_banner_box', {
        initialSlide: 1, direction: 'vertical', loop: false, allowSlideNext: true,
        on: {
            slideNextTransitionEnd: function() {
				$('#player')[0].pause();
				$('#my-video')[0].pause();
                obj.current++;	
				if(obj.current>obj.videoData.length-1){
					obj.current=0;
					var current2=obj.videoData.length-1;
					var current3=obj.current + 1;		
				}else if(obj.current<obj.videoData.length-1){
					var current2=obj.current - 1;
					var current3=obj.current + 1;
					if(current3>obj.videoData.length-1)current3=0;
				}else{
					var current2=obj.current - 1;
					var current3=obj.current + 1;
					if(current3>=obj.videoData.length)current3=0;
				}								
                obj.oneData = obj.videoData[current2];
				obj.twoData = obj.videoData[obj.current];
				obj.threeData = obj.videoData[current3];
                elemtVideo.prop('src', obj.twoData.video);
                elemtVideo.prop('poster', obj.twoData.poster);
				$("#nav_btn").addClass('loading');
				$("#videoid").val(obj.twoData.videoid);
				var key="videodetails_"+$("#videoid").val();
				if(localStorage.getItem(key)=="1"){
					$('.dianzan_btn').addClass('active');
				}else{
					$('.dianzan_btn').removeClass('active');
				}
				$(".pinglin_btn .txt").html(obj.twoData.videoviews);
				$(".dianzan_btn .txt").html(obj.twoData.viewzans);
				$("#nav_btn").attr("uid",obj.twoData.uid);
				$("#nav_btn .utitle").html('<img class="'+obj.twoData.video_uys+'" src="'+obj.twoData.video_uico+'">'+obj.twoData.videousername);
				$("#nav_btn .content").html(obj.twoData.videotitle);
				$(".header_pic_btn").attr("uid",obj.twoData.uid);
				$("#music_btn .img_banner img").attr("src",obj.twoData.videouserfilepath);
				$(".header_pic_btn .img_banner img").attr("src",obj.twoData.videouserfilepath);
                $('#NextEle').prop('src', obj.threeData.video);
                $('#NextEle').prop('poster', obj.threeData.poster);
				$('#NextEle').prop('style', "background-image: url("+obj.threeData.poster+");");
                if (obj.current > 0) { this.allowSlidePrev = true; }
                if (this.activeIndex == 2) { mySwiper2.slideTo(1, 0, false); }
                var nWidth = obj.videoData[obj.current].size.w;
                var nHeight = obj.videoData[obj.current].size.h;
                if (nHeight > nWidth) {
                    elemtVideo.css('object-fit', 'cover');
                } else {
                    elemtVideo.css('object-fit', 'contain');
                }
				getvideoxiangxi();
				if (isAndroid) { audioAutoPlay("player"); } else { audioAutoPlay("my-video"); }
				if(obj.current==obj.videoData.length-2 && obj.state=="0"){
					$.ajax({
						type: "post",dataType: "json",
						data: {"fid":0,"ids": obj.videoids,"webcode":obj.webcode},
						url: mobanurl+"?submitok=ajax_list&version="+new Date(),
						cache:false,
						success: function(d){							
							if (d.flag ==1) {
								obj.state=""+d.getstate; obj.videoids=d.videoids;
								if(d.list.length>0){ obj.videoData=obj.videoData.concat(d.list); }
							}else{
								this.allowSlidePrev = false;
							}
						}
					})
				}
           },
            slidePrevTransitionEnd: function() {
				$('#player')[0].pause();
				$('#my-video')[0].pause();
                obj.current--;
				if(obj.current<0){
					obj.current=obj.videoData.length - 1;
					var current2=obj.current - 1;
					var current3=0;
				}else if(obj.current==0){
					var current2=obj.videoData.length - 1;
					var current3=1;
				}else{
					var current2=obj.current - 1;
					var current3=obj.current + 1;
					if(current3>=obj.videoData.length - 1)current3=0;
				}
				obj.oneData = obj.videoData[current2];
                obj.twoData = obj.videoData[obj.current];
                obj.threeData = obj.videoData[current3];
                elemtVideo.prop('src', obj.twoData.video);
                elemtVideo.prop('poster', obj.twoData.poster);
				$("#nav_btn").addClass('loading');
				$("#videoid").val(obj.twoData.videoid);
				var key="videodetails_"+$("#videoid").val();
				if(localStorage.getItem(key)=="1"){
					$('.dianzan_btn').addClass('active');
				}else{	
					$('.dianzan_btn').removeClass('active');
				}
				$(".pinglin_btn .txt").html(obj.twoData.videoviews);
				$("#nav_btn").attr("uid",obj.twoData.uid);
				$("#nav_btn .utitle").html('<img class="'+obj.twoData.video_uys+'" src="'+obj.twoData.video_uico+'">'+obj.twoData.videousername);
				$("#nav_btn .content").html(obj.twoData.videotitle);
				$(".dianzan_btn .txt").html(obj.twoData.viewzans);
				$(".header_pic_btn").attr("uid",obj.twoData.uid);
				$(".header_pic_btn .img_banner img").attr("src",obj.twoData.videouserfilepath);
				$("#music_btn .img_banner img").attr("src",obj.twoData.videouserfilepath);
				rcmd_url=mobanurl.replace("{id}",obj.twoData.videoid);
				title=obj.twoData.videotitle;
				img_url=obj.twoData.poster;
				WeChat();
                $('#PrevEle').prop('style', "background-image: url("+obj.oneData.poster+");");
                $('#PrevEle').prop('src', obj.oneData.video);
                $('#PrevEle').prop('poster', obj.oneData.poster);
                $('#NextEle').prop('style', "background-image: url("+obj.threeData.poster+");");
                $('#NextEle').prop('poster', obj.threeData.poster);
                var nWidth = obj.videoData[obj.current].size.w;
                var nHeight = obj.videoData[obj.current].size.h;
                if (nHeight > nWidth) {
                    elemtVideo.css('object-fit', 'cover');
                } else {
                    elemtVideo.css('object-fit', 'contain');
                }
                if (obj.current < obj.videoData.length - 1) { this.allowSlideNext = true; }
                if (this.activeIndex == 0) { mySwiper2.slideTo(1, 0, false); }
                var nWidth = obj.videoData[obj.current].size.w;
                var nHeight = obj.videoData[obj.current].size.h;
                if (nHeight > nWidth) {
                    elemtVideo.css('object-fit', 'cover');
                } else {
                    elemtVideo.css('object-fit', 'contain');
                }
				getvideoxiangxi();
				if (isAndroid) {
					audioAutoPlay("player");
				} else {
					audioAutoPlay("my-video");
				}
            },
        }
    })	
});
var main={
	nextPageFavourableComments:function(page){
		$loading.show();
		if(jqueryXhr){jqueryXhr.abort();}
		setTimeout(function(){$loading.hide();}, 500);
	}
}